package com.cleaningcloud.cld.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.cleaningcloud.cld.domain.CldPayDetails;
import com.cleaningcloud.cld.domain.dto.CldCheckPerformanceDto;
import com.cleaningcloud.cld.domain.dto.CldCheckTemplateDto;
import com.cleaningcloud.cld.domain.vo.CldCheckPerformanceVo;
import com.cleaningcloud.cld.service.ICldPayDetailsService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldCheckPerformanceMapper;
import com.cleaningcloud.cld.domain.CldCheckPerformance;
import com.cleaningcloud.cld.service.ICldCheckPerformanceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 绩效考核Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-12
 */
@Service
public class CldCheckPerformanceServiceImpl implements ICldCheckPerformanceService 
{
    @Autowired
    private CldCheckPerformanceMapper cldCheckPerformanceMapper;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    /**
     * 查询绩效考核
     * 
     * @param performanceId 绩效考核主键
     * @return 绩效考核
     */
    @Override
    public CldCheckPerformanceVo selectCldCheckPerformanceByPerformanceId(Long performanceId)
    {
        return cldCheckPerformanceMapper.selectCldCheckPerformanceByPerformanceId(performanceId);
    }

    /**
     * 查询绩效考核列表
     * 
     * @param cldCheckPerformance 绩效考核
     * @return 绩效考核
     */
    @Override
    public List<CldCheckPerformanceVo> selectCldCheckPerformanceList(CldCheckPerformance cldCheckPerformance)
    {
        List<CldCheckPerformanceVo> list = cldCheckPerformanceMapper.selectCldCheckPerformanceList(cldCheckPerformance);
        for (CldCheckPerformanceVo cldCheckPerformanceVo : list) {
            cldCheckPerformanceVo.setResultList(cldCheckPerformanceMapper.selectCldCheckResultByPerformanceId(cldCheckPerformanceVo.getPerformanceId()));
        }
        return list;
    }

    /**
     * 新增绩效考核
     * 
     * @param cldCheckPerformance 绩效考核
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldCheckPerformance(CldCheckPerformanceDto cldCheckPerformanceDto)
    {
        cldCheckPerformanceDto.setCreateTime(DateUtils.getNowDate());
        cldCheckPerformanceDto.setCreateId(SecurityUtils.getUserId());
        cldCheckPerformanceDto.setCreateName(SecurityUtils.getUsername());
        cldCheckPerformanceDto.setFlag(0L);
        cldCheckPerformanceMapper.insertCldCheckPerformance(cldCheckPerformanceDto);
        // 新增考核结果明细
        batchCheckResultInsert(cldCheckPerformanceDto);
        // 计算绩效考核
//        calculatePerformanceAppraisal(cldCheckPerformanceDto);
        return 1;
    }

    /**
     * 修改绩效考核
     * 
     * @param cldCheckPerformance 绩效考核
     * @return 结果
     */
    @Override
    public int updateCldCheckPerformance(CldCheckPerformanceDto cldCheckPerformanceDto)
    {
        cldCheckPerformanceDto.setUpdateTime(DateUtils.getNowDate());
        cldCheckPerformanceDto.setUpdateId(SecurityUtils.getUserId());
        cldCheckPerformanceDto.setUpdateName(SecurityUtils.getUsername());
        // 删除之前绑定
        cldCheckPerformanceMapper.deleteCheckResultByPerformanceId(cldCheckPerformanceDto.getPerformanceId());
        // 新增考核结果明细
        batchCheckResultInsert(cldCheckPerformanceDto);
        return cldCheckPerformanceMapper.updateCldCheckPerformance(cldCheckPerformanceDto);
    }

    /**
     * 计算绩效考核薪酬明细
     * @param cldCheckPerformanceDto
     */
    public void calculatePerformanceAppraisal(CldCheckPerformanceDto cldCheckPerformanceDto){
        // 计算绩效考核薪酬
        BigDecimal money = cldCheckPerformanceDto.getPerformanceScore().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).multiply(cldCheckPerformanceDto.getCardinality());
        CldPayDetails cldPayDetails = new CldPayDetails();
        cldPayDetails.setIsAlgorithm(1L);
        cldPayDetails.setPayType(2L);
        cldPayDetails.setStaffId(cldCheckPerformanceDto.getCheckStaffId());
        cldPayDetails.setProjectId(cldCheckPerformanceDto.getProjectId());
        cldPayDetails.setProjectPostId(cldCheckPerformanceDto.getProjectPostName());
        cldPayDetails.setMoney(money);
        cldPayDetails.setBusinessId(cldCheckPerformanceDto.getPerformanceId());
        cldPayDetailsService.insertCldPayDetails(cldPayDetails);
    }

    public void batchCheckResultInsert(CldCheckPerformanceDto cldCheckPerformanceDto){
        if(cldCheckPerformanceDto.getResultList() != null && !cldCheckPerformanceDto.getResultList().isEmpty()) {
            cldCheckPerformanceDto.getResultList().forEach( item -> {
                item.setPerformanceId(cldCheckPerformanceDto.getPerformanceId());
            });
            cldCheckPerformanceMapper.batchCheckResultInsert(cldCheckPerformanceDto.getResultList());
        }
    }
    /**
     * 批量删除绩效考核
     * 
     * @param performanceIds 需要删除的绩效考核主键
     * @return 结果
     */
    @Override
    public int deleteCldCheckPerformanceByPerformanceIds(Long[] performanceIds)
    {
        return cldCheckPerformanceMapper.deleteCldCheckPerformanceByPerformanceIds(performanceIds);
    }

    /**
     * 删除绩效考核信息
     * 
     * @param performanceId 绩效考核主键
     * @return 结果
     */
    @Override
    public int deleteCldCheckPerformanceByPerformanceId(Long performanceId)
    {
        return cldCheckPerformanceMapper.deleteCldCheckPerformanceByPerformanceId(performanceId);
    }
}
